#include "dlg_common_frame.h"
#include "ui_dlg_common_frame.h"

Dlg_common_frame::Dlg_common_frame(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg_common_frame)
{
    ui->setupUi(this);

    this->setWindowFlags(this->windowFlags()| Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
    ui->tableView->setModel(model_common_7E);//sql模型与tableView绑定
    ui->tableView->resizeColumnsToContents();//列宽适应内容
    ui->tableView->setSortingEnabled(true);//排序使能
    //ui->tableView->verticalHeader()->setSectionsMovable(true);//按住行号可拖动排序
    ui->tableView->hideColumn(0);//隐藏主键列


    QObject::connect(ui->tableView->verticalHeader(),SIGNAL(sectionMoved(int,int,int)),
                     this,SLOT(on_verticalHeader_sectionMoved(int,int,int)));//行的视图索引变化信号的处理

}
void Dlg_common_frame::on_verticalHeader_sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
{
    qDebug()<<"逻辑行号(传参):"<<logicalIndex;
    qDebug()<<"新视图号->逻辑行号:"<<newVisualIndex <<"->"<<ui->tableView->verticalHeader()->logicalIndex(newVisualIndex);
    qDebug()<<"旧视图号->逻辑行号:"<<oldVisualIndex <<"->"<<ui->tableView->verticalHeader()->logicalIndex(oldVisualIndex)<<"\n";
    //程序结束前。行的逻辑号不会变

    QModelIndex index = model_common_7E->index(logicalIndex,0);

    model_common_7E->setEditStrategy(QSqlTableModel::OnManualSubmit);//设定何时将更改更新到数据库？

    int rowCount = model_common_7E->rowCount();
    for(int visu_idx = 0; visu_idx < rowCount; visu_idx++)
    {
        int logical_idx = ui->tableView->verticalHeader()->logicalIndex(visu_idx);
        index = model_common_7E->index(logical_idx,0);//第logical_idx行第key列
        model_common_7E->setData(index,0x8000|visu_idx);
    }
    model_common_7E->submitAll();
    for(int visu_idx = 0; visu_idx < rowCount; visu_idx++)
    {
        int logical_idx = ui->tableView->verticalHeader()->logicalIndex(visu_idx);
        index = model_common_7E->index(logical_idx,0);//第logical_idx行第key列
        model_common_7E->setData(index,visu_idx);
    }
    model_common_7E->submitAll();

//    model_common_7E->submitAll();
//    model_common_7E->setEditStrategy(QSqlTableModel::OnFieldChange);//设定何时将更改更新到数据库？


    //    int drag_pos_logic = logicalIndex;//鼠标拖动的行的逻辑号
//    int drop_pos_logic = ui->tableView->verticalHeader()->logicalIndex(newVisualIndex);//鼠标释放的行的逻辑号

//    int columun_idx = model_common_7E->fieldIndex("key");

//    QModelIndex index = model_common_7E->index(drag_pos_logic,columun_idx);
//    int drag_pos_key = model_common_7E->data(index).toInt();

//    index = model_common_7E->index(drop_pos_logic,columun_idx);
//    int drop_pos_key = model_common_7E->data(index).toInt();

//    qDebug()<<"拖动位置的行的key:"<<drag_pos_key;
//    qDebug()<<"释放位置的行的key:"<<drop_pos_key;

//    model_common_7E->setData(model_common_7E->index(drag_pos_logic,columun_idx),10000);
//    model_common_7E->setData(model_common_7E->index(drop_pos_logic,columun_idx),10001);

//    model_common_7E->setData(model_common_7E->index(drag_pos_logic,columun_idx),drop_pos_key);
//    model_common_7E->setData(model_common_7E->index(drop_pos_logic,columun_idx),drag_pos_key);






//    int columun_idx = model_common_7E->fieldIndex("key");
//    QModelIndex index;


//    for(int db_row=0;db_row < model_common_7E->rowCount();db_row++)
//    {
//        index = model_common_7E->index(db_row,columun_idx);
//        qDebug()<<"修改key的结果:"<<model_common_7E->setData(index,db_row+1000);
//    }

//    for(int db_row=0;db_row < model_common_7E->rowCount();db_row++)
//    {
//        index = model_common_7E->index(db_row,columun_idx);
//        qDebug()<<"修改key的结果:"<<model_common_7E->setData(index,db_row);
//    }

    //model_common_7E->setFilter("");//相当于刷新tableView的显示

}

Dlg_common_frame::~Dlg_common_frame()
{
    delete ui;
}


void Dlg_common_frame::on_pushButtonDelRow_clicked()
{
    qDebug()<<"要删除的行："<<ui->tableView->currentIndex().row();
    qDebug()<<"删除操作的结果是："<<model_common_7E->removeRow(ui->tableView->currentIndex().row());
    qDebug()<<"提交删除的结果是："<<model_common_7E->submitAll();//提交修改
    model_common_7E->setFilter("");//相当于刷新tableView的显示
}
//void Dlg_common_frame::on_Act_DelRow_triggered()
//{
//    qDebug()<<"要删除的行："<<ui->tableView->currentIndex().row();
//    qDebug()<<"删除操作的结果是："<<model_common_7E->removeRow(ui->tableView->currentIndex().row());
//    qDebug()<<"提交删除的结果是："<<model_common_7E->submitAll();//提交修改
//    model_common_7E->setFilter("");//相当于刷新tableView的显示
//}

void Dlg_common_frame::on_pushButtonSave_clicked()
{

    if(model_common_7E->submitAll())
    {
        qDebug()<<"保存成功";
    }
    else
    {
        qDebug()<<"保存失败:"<<model_common_7E->lastError();
    }

    model_common_7E->setFilter("");//相当于刷新tableView的显示

}
